package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.event.EventObject;
import me.shurufa.fragments.AboutBookCommentFragment;
import me.shurufa.fragments.AboutBookInfoFragment;
import me.shurufa.fragments.AboutBookMarkFragment;
import me.shurufa.fragments.MyAboutBookCommentFragment;
import me.shurufa.fragments.MyAboutBookMarkFragment;
import me.shurufa.model.Digest;
import me.shurufa.utils.Constants;
import me.shurufa.widget.popupwindow.AddCommentWindow;
import me.shurufa.widget.popupwindow.AddNoteWindow;

/**
 * 发现页点击图书跳转页面
 *
 * @FileName: me.shurufa.activities.AboutBookActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-22 09:59
 */
public class AboutBookActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.right_text)
    TextView rightText;

    private long mBookId;
    private AddCommentWindow editDigestWindow;
    private AddNoteWindow addNoteWindow;
    private int fromWhere;
    private long mExcerptedBookId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_about_book;
        super.onCreate(savedInstanceState);
        mBookId = getIntent().getLongExtra(Constants.ARG_BOOK_ID,0L);
        mExcerptedBookId = getIntent().getLongExtra(Constants.ARG_EXCERPT_BOOKID,0L);
        fromWhere = getIntent().getIntExtra(Constants.ARG_FROM_WHERE,0);
        initUI();
        initData();
    }

    private void initData() {
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES){
            AboutBookInfoFragment bookInfoFragment = AboutBookInfoFragment.newInstance(mBookId);
            MyAboutBookCommentFragment bookCommentFragment = MyAboutBookCommentFragment.newInstance(mBookId);
            MyAboutBookMarkFragment bookMarkFragment = MyAboutBookMarkFragment.newInstance(mExcerptedBookId,mBookId);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.bookinfo_container,bookInfoFragment);
            transaction.add(R.id.book_comment_container,bookCommentFragment);
            transaction.add(R.id.book_mark_container,bookMarkFragment);
            transaction.commit();
        }else {
            AboutBookInfoFragment bookInfoFragment = AboutBookInfoFragment.newInstance(mBookId);
            AboutBookMarkFragment bookMarkFragment = AboutBookMarkFragment.newInstance(mBookId);
            AboutBookCommentFragment bookCommentFragment = AboutBookCommentFragment.newInstance(mBookId);
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.add(R.id.bookinfo_container, bookInfoFragment);
            transaction.add(R.id.book_mark_container, bookMarkFragment);
            transaction.add(R.id.book_comment_container, bookCommentFragment);
            transaction.commit();
        }
    }

    private void initUI() {
        editDigestWindow = new AddCommentWindow(this);
        addNoteWindow = new AddNoteWindow(this);
        rightText.setVisibility(View.VISIBLE);
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES)
            rightText.setText(getString(R.string.add_digest));
        else
            rightText.setText(getString(R.string.evaluate_this_book));
        rightText.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.right_text:
                if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES){
                    intent = new Intent(AboutBookActivity.this,CameraActivity.class);
                    intent.putExtra(Constants.ARG_BOOK_ID,mBookId);
                    startActivity(intent);
                }else {
                    editDigestWindow.init(mBookId, Constants.TYPE_COMMENT_BOOK);
                    editDigestWindow.showMoreWindow(v, 100);
                }
                break;
        }
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (intent.getAction().equals(Constants.ACTION_BOOKINFO_ADD_COMMENT)){
            editDigestWindow.init(mBookId, Constants.TYPE_COMMENT_BOOK);
            editDigestWindow.showMoreWindow(rightText, 100);
        }else if (intent.getAction().equals(Constants.ACTION_ADD_DIGEST_COMPLETE)){
            finish();
        }
    }

    public void onEventMainThread(EventObject object){
        if (object.getEventAction().equals(Constants.ACTION_WRITE_NOTE)){
            Digest digest = object.getDigest();
            if (digest!=null&&addNoteWindow!=null){
                addNoteWindow.init(digest);
                addNoteWindow.showMoreWindow(rightText,100);
            }
        }
    }
}
